<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      .content {
        overflow: hidden;
        width: 1000px;
        height: 3000px;
        background-color: pink;
        margin: 0 auto;
      }

      .backtop {
        display: none;
        width: 50px;
        left: 50%;
        margin: 0 0 0 505px;
        position: fixed;
        bottom: 60px;
        z-index: 100;
      }

      .backtop a {
        height: 50px;
        width: 50px;
        background: url(./images/bg2.png) 0 -600px no-repeat;
        opacity: 0.35;
        overflow: hidden;
        display: block;
        text-indent: -999em;
        cursor: pointer;
      }

      .header {
        position: fixed;
        top: -80px;
        left: 0;
        width: 100%;
        height: 80px;
        background-color: purple;
        text-align: center;
        color: #fff;
        line-height: 80px;
        font-size: 30px;
        transition: all 0.3s;
      }

      .sk {
        width: 300px;
        height: 300px;
        background-color: skyblue;
        margin-top: 500px;
      }
    </style>
  </head>

  <body>
    <div class="header">我是顶部导航栏</div>
    <div class="content">
      <div class="sk">秒杀模块</div>
    </div>
    <div class="backtop">
      <img src="./images/close2.png" alt="" />
      <a href="javascript:;"></a>
    </div>
    <script>
      //获取元素
      const sk = document.querySelector('.sk')
      const header = document.querySelector('.header')
      const backtop = document.querySelector('.backtop')

      let flag = false

      //绑定滚动事件
      window.addEventListener('scroll', function () {
        const n = document.documentElement.scrollTop

        // 卷去部分
        // console.log(n)

        // 模块距离顶部距离
        // console.log(sk.offsetTop)

        //判断卷去部分大于模块距离顶部距离与否
        header.style.top = n >= sk.offsetTop ? 0 : '-80px'

        // console.log(flag && n >= sk.offsetTop ? 'block' : 'none')
        backtop.style.display = flag && n >= sk.offsetTop ? 'block' : 'none'
      })

      //绑定回到顶部事件
      backtop.addEventListener('click', function (e) {
        // 真正的触发者
        // console.log(e.target)
        if (e.target.tagName === 'A') {
          //   console.log(1)
          // 回到顶部
          window.scrollTo(0, 0)
        }

        if (e.target.tagName === 'IMG') {
          //关闭按钮
          this.style.display = 'none'
          flag = false
        }
      })
    </script>
  </body>
</html>
